<template>
    <el-form ref="form" :model="user" :rules="rules" label-width="100px">
      <el-form-item label="当前参赛组别" prop="oldGroup">
        <el-select v-model="user.userGroup" placeholder="请选择参赛组别" disabled >
          <el-option
            v-for="dict in userGroupOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="新参赛组别" prop="newGroup">
        <el-select v-model="user.newGroup" placeholder="请选择新参赛组别" clearable >
          <el-option
            v-for="dict in userGroupOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="submit">确定变更参赛组别</el-button>
        <el-button type="danger" size="mini" @click="close">关闭</el-button>
      </el-form-item>
    </el-form>
</template>

<script>
import { updateUserProfile } from "@/api/system/user";

export default {
  props: {
    user: {
      type: Object
    }
  },
  data() {
    return {
      userGroupOptions: undefined,
      user: {
        oldGroup: undefined,
        newGroup: undefined
      },
      // 表单校验
      rules: {
        newGroup: [
          {required: true, message: "新参赛组别不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getDicts("group_type").then(response => {
      this.userGroupOptions = response.data;
    });
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.user.userGroup = this.user.newGroup;
            updateUserProfile(this.user).then(response => {
              this.$confirm('修改参赛组别成功，请重新登录完成更新！', "提示", {
                confirmButtonText: "好的，现在重新登录",
                cancelButtonText: "暂不重新登录",
                type: "success"
              }).then(() => {
                this.$store.dispatch('LogOut').then(() => {
                  location.href = '/index';
                })
              }).catch(() => {});
              // this.$modal.msgSuccess("修改参赛组别成功，请退出重新登录完成更新！");
            });
        }
      });
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>
